<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/6
 * Time: 17:35
 */

namespace app\admin\controller;


use app\reuse\controller\ResponseJson;
use Redis\Redis;
use think\App;
use think\Db;

class DataCenter extends Common
{
    use ResponseJson;

    public function __construct(App $app = null)
    {
        parent::__construct();
    }

    function index()
    {
        /**********平台数据**********/
        # 意见反馈
        $feedbackNum = Db::table('feedback')->count();
        $this->assign('feedback', $feedbackNum);
        # 交易审核
        $tradAuditNum = Db::table('trad_audit')->where(array('del_status' => 1, 'trad_status' => 1))->count();
        $this->assign('tradAudit', $tradAuditNum);
        # 玩家投诉
        $complaintNum = Db::table('complaint')->where('status = 1')->count();
        $this->assign('complaint', $complaintNum);
        # 合伙人申请
        $partnerNum = Db::table('partner_audit')->where('status != -1')->count();
        $this->assign('partner', $partnerNum);
        # 当前在线人数(从redis中获取)
        $onlinePeopleNum = Db::table('log_login')->where('(' . time() . '-UNIX_TIMESTAMP(change_time))<3600 and date(create_time) = "' . DATESTAMP . '"')->count();
        $this->assign('onlinePeopleNum', $onlinePeopleNum);
        # 日下载量
        $downloadNum = Db::connect("db_config2")->table("download_info")->where([['time','>=',date('Y-m-d 00:00:00')]])->count();
        $this->assign('downloadNum', $downloadNum);
        /**********玩家数据**********/
        #总玩家
        $membeNum = Db::table('member')->count();
        $this->assign('memberNum', $membeNum);
        # 今日新增用户
        $memberNewTodayNum = Db::table('member')->where("date(register_time) = '" . date('Y-m-d') . "'")->count();
        $this->assign('memberNewTodayNum', $memberNewTodayNum);
        # 已退出玩家
        $memberExitedNum = Db::table('member')->where('type in (3,4)')->count();
        $this->assign('memberExitedNum', $memberExitedNum);
        # 性别分析
        $sexInfo = Db::table('member')->field('count(id) count')->group('sex')->order(array('sex' => 'asc'))->select();
        $this->assign('sexInfo', array('men' => $sexInfo[0]['count'], 'women' => $sexInfo[1]['count']));
        # 团队数量
        $teamNum = Db::table('team')->where(array('is_fake' => 1))->count();
        $this->assign('teamNum', $teamNum);
        /**********资产数据**********/
        # 当前生意总数
        $currentBusinessNum = Db::table('member_business')->where('status != -1')->count();
        $this->assign('currentBusinessNum', $currentBusinessNum);
        # 今日玩家总收益
        $playerRevenueTodayNum = Db::table('log_earn')->where("type != -1 and date(create_time) = '" . date('Y-m-d') . "'")->sum('income');
        $this->assign('playerRevenueTodayNum', $playerRevenueTodayNum);
        # 当前金币总量
        $goldNum = Db::table('member_property')->where('is_fake=1')->sum('gold');
        $this->assign('goldNum', $goldNum);
        # 今日金币计划用量
        $redis=new Redis();
        $planGoldNum=$redis->hGet("planGoldNum","planGoldNum");
        if(empty($planGoldNum)){
            $planGoldNum = Db::table('member_business mb')
                ->where(array('mb.status' => 4))
                ->where("mb.status=4 or (mb.status!=-1 and date(mb.opera_start_time)<='".date("Y-m-d",strtotime('-1 day'))."' and mb.rent_day <mb.growth_cycle)")
                ->sum('mb.day_rents');
            $time=strtotime(date("Y-m-d 23:59:59"))-time();
            $redis->hSet("planGoldNum","planGoldNum",$planGoldNum,$time);
        }
        $this->assign('planGoldNum', $planGoldNum);
        # 当前积分总量
        $integralNum = Db::table('member_property')->where('is_fake=1')->sum('integral');
        $this->assign('integralNum', $integralNum);
        # 恢复材料总数
        $recoveredMaterialsNum = Db::table('member_property')->where('is_fake=1')->sum('materials');
        $this->assign('recoveredMaterialsNum', $recoveredMaterialsNum);
        # 红宝石总数
        $rubyNum = Db::table('member_property')->where('is_fake=1')->sum('ruby');
        $this->assign('rubyNum', $rubyNum);
        /**********交易数据**********/
        # 今日交易成功数
        $todayTransactionSuccessNum = Db::table('trad_audit')->where("trad_status != 1 and is_violations=1 and date(trad_time)='" . date('Y-m-d') . "'")->count();
        $this->assign('todayTransactionSuccessNum', $todayTransactionSuccessNum);
        # 昨日交易成功数
        $yesterdayTransactionSuccessNum = Db::table('trad_audit')->where("trad_status != 1 and is_violations=1 and date(trad_time)='" . date('Y-m-d', strtotime('-1 day')) . "'")->count();
        $this->assign('yesterdayTransactionSuccessNum', $yesterdayTransactionSuccessNum);
        # 待匹配数据分析(待出售比例 = 待出售数量/总数量)
        $forSaleNum = Db::table('member_business')->where('status = 2 or status=3')->count();//待出售数量
        if ($currentBusinessNum > 0) {
            $proportionToBeSold = priceFormat($forSaleNum / $currentBusinessNum * 100) . '%';
        } else {
            $proportionToBeSold = '0%';
        }
        $this->assign('proportionToBeSold', $proportionToBeSold);
        # 赠送金币数量
        $giveGoldNum = Db::table('log_gold')->where('type in (3,4)')->sum('gold_num');
        $this->assign('giveGoldNum', $giveGoldNum);
        # 赠送积分数量
        $giveIntegralNum = Db::table('log_integral')->where('type in (3,4)')->sum('num');
        $this->assign('giveIntegralNum', $giveIntegralNum);
        return $this->fetch('/index/dataCenter');
    }
}